package com.neborosoft.lib.collections.list;

import com.neborosoft.lib.API_8.Collections;
import org.apache.commons.collections15.list.SetUniqueList;

import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;

/**
 * Project: FlyingDog
 * User: stikhonenko
 * Date: 12/10/12
 * Time: 6:23 PM
 */
public final class Lists {
    public static  <T> SetUniqueList<T> toSetUniqueList(List<T> list){
        if(list instanceof SetUniqueList){
            return (SetUniqueList<T>)list;
        }
        else {
            return SetUniqueList.decorate(list);
        }
    }

    public static  <T> List<Integer> getSortedListIndexingMap(final List<T> list, final Comparator<T> comparator){
        List<Integer> indexingMap = new ArrayList<Integer>();
        for(int i = 0; i < indexingMap.size(); i++){
            indexingMap.set(i,i);
        }

        Collections.sort(indexingMap, new Comparator<Integer>() {
            @Override
            public int compare(Integer integer, Integer integer2) {
                T a = list.get(integer);
                T b = list.get(integer2);
                return comparator.compare(a,b);
            }
        });

        return indexingMap;
    }

    public static  <T extends Comparable> List<Integer> getSortedListIndexingMap(final List<T> list){
        return getSortedListIndexingMap(list, new Comparator<T>() {
            @Override
            public int compare(T lhs, T rhs) {
                return lhs.compareTo(rhs);
            }
        });
    }

    public static <T> void sortWithIndexesComparator(final List<T> list, final Comparator<Integer> comparator){
        Collections.sort(list, new Comparator<T>() {
            @Override
            public int compare(T lhs, T rhs) {
                return comparator.compare(list.indexOf(lhs),list.indexOf(rhs));
            }
        });
    }

    public static Integer incrementAndGet(List<Integer> list, int index){
        Integer integer = list.get(index);
        return list.set(index, integer + 1);
    }
}
